﻿
Imports std
Imports tcpServer4

Public Class rcserve4
    Inherits _rcserve(Of eventComb, tcpServer4, tcpServer4.talkHandler)

    Private Sub server_receive(ByVal handler As talkHandler, ByRef ec As eventComb) Handles server.receive
        assert(Not handler Is Nothing)
        assert(ec Is Nothing)
        Dim rtns As pointer(Of toBytes()) = Nothing
        Dim invokeec As eventComb = Nothing
        Dim types() As Type = Nothing
        If invoke(handler, rtns, invokeec, types) Then
            ec = New eventComb(Function() As Boolean
                                   Return (invokeec Is Nothing OrElse waitfor(invokeec)) AndAlso [goto](1)
                               End Function,
                               Function() As Boolean
                                   If invokeec Is Nothing OrElse invokeec.end_result() Then
                                       Dim rtn As nettransData = Nothing
                                       rtn = New nettransData()
                                       If protocol.write(rtn, +rtns, isemptyarray(types)) Then
                                           Dim rtnec As eventComb = Nothing
                                           Return handler.return(rtn, rtnec) AndAlso
                                                  waitfor(rtnec) AndAlso
                                                  goto_end()
                                       Else
                                           raiseError(errorHandle.errorType.warning,
                                                      "cannot write response to return data for request ",
                                                      Convert.ToString(handler))
                                           Return False
                                       End If
                                   Else
                                       Return False
                                   End If
                               End Function)
        End If
    End Sub

    Public Shared Function unittestInvoke(ByVal arg As fromBytes) As toBytes
        Dim rtn As bytesInt64 = Nothing
        If istype(Of bytesInt64)(arg) OrElse istype(Of bytesbytes)(arg) Then
            Return arg
        Else
            Return New bytesInt64(rnd(0, strlen(Convert.ToString(arg)), True))
        End If
    End Function

    Private Shared Function unittestInvoke(ByRef args() As fromBytes,
                                           ByRef prtns As pointer(Of toBytes())) As eventComb
        Dim rtns() As toBytes = Nothing
        ReDim rtns(0)
        rtns(0) = unittestInvoke(args(0))
        If Not prtns Is Nothing Then
            prtns.set(rtns)
        End If
        Return Nothing
    End Function

    Public Shared Sub unittest()
        Dim rcserver As rcserve4 = Nothing
        Try
            raiseError("********** rcserve4 unitTest **********", errorHandle.errorType.application)

            rcserver = New rcserve4()
            rcserver.register(share.unittest.unittestInvokeName, AddressOf unittestInvoke)
            rcserver.start(share.unittest.port)
            While Not consoleExtSignal()
                sleep()
            End While
            rcserver.stop()

            raiseError("*********** finish unitTest ***********", errorHandle.errorType.application)
        Finally
            'for remotecall performance test, the thread will be killed after test finished.
            If Not rcserver Is Nothing Then
                rcserver.stop(0)
                'immediately stop server, as tcp connections cannot be released before app stops.
            End If

            raiseError("*********** finish unitTest ***********", errorHandle.errorType.application)
        End Try
    End Sub
End Class
